package in.muton.edushare.components.tasks.electives.api;

import java.util.HashMap;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.Courses;

public class SerialChecker {
	public static final Log log = LogFactory.getLog(SerialChecker.class);
	public boolean checkElectivePreference(HashMap<IModel<Courses>, TextField<String>> map){
		int size = map.size();
		int [] a = new int[size];
		log.debug("Size of the selection box is "+size);
		Set<Entry<IModel<Courses>, TextField<String>>> set = map.entrySet();
		for(Entry<IModel<Courses>, TextField<String>> e:set){
			try{
				log.debug("Option = " + e.getValue().getModel().getObject());
				int option = Integer.parseInt(e.getValue().getModel().getObject());
				if(option <1 || option > size){
					throw new NumberFormatException();
				}
				if(a[option - 1] == 0){
					a[option - 1] = 1;
				}else
					throw new NumberFormatException();
			}catch(NumberFormatException nu){
				setError(true);
				setErrorMessage("Please indicate your choice using a number between 1 and "+size);
				return false;
			}
		}
		return true;
	}
	
	public void setError(boolean error) {
		this.error = error;
	}
	public boolean isError() {
		return error;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	private boolean error = false;
	private String errorMessage = "";
}
